package UIFree.components
{
	import UIFInterface.ILabel;
	import UIFInterface.IWindow;
	
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_window extends uif_ecomponent implements IWindow {

		protected var _title		: uif_label;
		protected var _close		: uif_component;
		protected var _canClose		: int;
		protected var _isModal		: Boolean;
		
		public static const CLOSEWINDOW		: String ='CLOSEWINDOW';
		
		public static const CLOSEDISPOSE	: int = 2;
		public static const CLOSEHIDE		: int = 1;
		
		protected static var _modalGurtain	: uif_ecomponent;
		
		public function uif_window(parent:uif_component, ax:int=0, ay:int=0, 
									awidth:int=0, aheight:int=0, styleClass:String='', 
									modal:Boolean=false, canClose: int = CLOSEDISPOSE) {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			alpha = 0;
			_canClose = canClose;
			if (_isModal = modal) asModal();
			_title = createTitle();
			_title.addEventListener(MouseEvent.MOUSE_DOWN, doTitleMouseDown);
			createDialogComponents();
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEventListener(Event.CLOSE, doClose);
		}
		
		protected function createTitle(): uif_label {
			var inner: Rectangle = titleRect;
			var a_title: uif_label = new uif_label(this, inner.x, inner.y, inner.width, inner.height, style['title']);
			a_title.textField.height = 25;
			return a_title;
		}
		
		protected function doTitleMouseDown(e: MouseEvent): void {
			index = parent.numChildren - 1;
			beginDrag(5);
		}
		
		protected function get titleRect(): Rectangle {
			var inner: Rectangle = getInner();
			inner.x 		= inner.width / 40;
			inner.y 		= -18;
			inner.width 	= inner.width - inner.width / 3;
			inner.height 	= 25;
			return inner;
		}
		
		override public function resize(newWidth: Number, newHeight: Number): uif_component {
			super.resize(newWidth, newHeight);
			if (_title) {
				var tRect: Rectangle = titleRect;
				_title.resize(tRect.width, tRect.height);
			}
			if (_close) {
				_close.x = width - 30;
			}
			return this;
		}
		
		
		protected function createDialogComponents(): void {
			_close = createCloseButton();
		}
		
		protected function createCloseButton(): uif_component {
			return new uif_close(this);
		}
		
		protected function createModalGuard(): uif_ecomponent {
			var inner: Rectangle = app.getInner();
			return new uif_ecomponent(parent as uif_component, inner.x, inner.y, inner.width, inner.height, 
				style['gurtain']?style['gurtain']:'modalGurtain');
		}
		
		protected function get isModal(): Boolean {
			return _isModal;
		}
		
		protected function asModal(): void {
			if (!_modalGurtain) {
				_modalGurtain = createModalGuard();
				_modalGurtain.visible = false;
				_modalGurtain.alpha = 0;
	//			parent.swapChildren(_modalGurtain, this);
			}
			_modalGurtain.index = 0;
		}
		
		protected function otherModalVisible(): Boolean {
			var count: int = 0;
			parentComponent.forEach(function(window: uif_window): void {
				if ((window != This) && window.isModal && window.visible) count++;
			}, uif_window);
			return count > 0;
		}
		
		public function show(onShow: Function = null): void {
			slideShow(onShow);
			if (_isModal) _modalGurtain.slideShow();
		}
		
		protected function doClose(e: Event): void {
			if (parent != null) close();
		}
		
		public function close(canCloseType: int=0, onClose: Function=null): void {
			function afterHide(): void {
				dispatchEvent(new Event(CLOSEWINDOW));
				if (onClose != null) onClose();
				if (canCloseType == CLOSEDISPOSE) dispose();
			}
			canCloseType = canCloseType?canCloseType:_canClose;
			slideHide(afterHide);
			if (!otherModalVisible()) _modalGurtain.slideHide();
		}
		
		protected function canClose(): int {
			return _canClose;
		}
		
		public function get title(): ILabel {
			return _title;
		}
	}
}